#include "raw_data.h"
#include "public_define.h"

#include <iostream>

using namespace std;

namespace caviz
{
  RawData::RawData(const std::string &raw_data)
  {
    int len = raw_data.length() - headSize_;
    meta_data_.resize(len);
    memcpy(&head_, raw_data.data(), headSize_);
    memcpy(&meta_data_[0], (const void *)&raw_data[headSize_], len);
    name_ = head_.name;
  }

  RawData::RawData(const std::string &name, double timestamp,
                   const std::string &meta_data)
      : name_(name), meta_data_(meta_data)
  {
    if (name.length() > MAX_NAME_SIZE)
    {
      Log_Info("data name length must less than MAX_NAME_SIZE {}", to_string(MAX_NAME_SIZE));
      return;
    }

    memcpy(head_.name, &name[0], name.length() + 1);
    head_.size = meta_data.length();
    head_.timestamp = timestamp;
    raw_data_.resize(headSize_ + meta_data.length());

    memcpy(&raw_data_[0], &head_, headSize_);
    memcpy((void *)&raw_data_[headSize_], meta_data_.data(), head_.size);
  }

  void RawData::GetRawDataHeader(std::string &header, 
                                const std::string &name, 
                                double timestamp, 
                                const std::string &meta_data) 
  {
    if (name.length() > MAX_NAME_SIZE)
    {
      Log_Info("2 data name length must less than MAX_NAME_SIZE {}", to_string(MAX_NAME_SIZE));
      return;
    }

    RawDataHead tmp_header;
    memcpy(tmp_header.name, &name[0], name.length() + 1);
    tmp_header.size = meta_data.length();
    tmp_header.timestamp = timestamp;

    header.resize(sizeof(RawDataHead));
    memcpy(&header[0], &tmp_header, sizeof(RawDataHead));
  }

} // namespace caviz